// 设备定义--阶段参数
<template>
  <div class="item1">
    <!-- <div class="breadcrumb" :gutter="24"> -->
    <div class="btngroup" style="padding-right:15px;text-align:right;margin-bottom:10px">
      <!-- <el-button type="primary" size="small" @click="handleAdd()">{{ $t("common.add") }}</el-button> -->
      <el-button type="primary" size="small" @click="handleEdit()"
        v-if="this.phaseType != 9 && this.phaseType != 10">{{ $t("common.edit") }}</el-button>
      <!-- <el-button type="primary" size="small" @click="handleDel()">{{ $t("common.remove") }}</el-button> -->
    </div>
    <!-- </div> -->
    <!-- 表格区域 -->
    <div class="tableAuto">
      <el-table :data="tableData" style="width:100%" :height="height" @row-click="rowClick" border stripe
        :header-cell-style="{background:'#EAEBEC'}" v-loading="tableLoading" ref="refTable" highlight-current-row @sort-change='sortChange'>
        <el-table-column label="" width="50" align="center">
          <template slot-scope="scope">
            <div>
              <el-radio :label="scope.row.fid" v-model="childId">&nbsp;</el-radio>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" :label="$t('common.number')" width="90" align="center">
          <template slot-scope="scope">
            {{table_index(scope.$index+1)}}
          </template>
        </el-table-column>
        <el-table-column prop="name" show-overflow-tooltip :label="$t('basic.argu')" width="300">
          <template slot-scope="scope">
            {{scope.row.phaseTagDefineVo ? scope.row.phaseTagDefineVo.name : ""}}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="wr" show-overflow-tooltip label="读写" width="100">
          <template slot-scope="scope">
            {{scope.row.phaseTagDefineVo ? getwrName(scope.row.phaseTagDefineVo.wr) : ""}}
          </template>
        </el-table-column> -->
        <el-table-column prop="type" show-overflow-tooltip :label="$t('basic.data_type')" width="200">
          <template slot-scope="scope">
            {{scope.row.phaseTagDefineVo ? getDataType(scope.row.phaseTagDefineVo.type) : ""}}
          </template>
        </el-table-column>
        <el-table-column prop="uom" show-overflow-tooltip :label="$t('basic.company')" width="180">
          <template slot-scope="scope">
            {{scope.row.phaseTagDefineVo ? scope.row.phaseTagDefineVo.uom : ""}}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="hihiAddr"  show-overflow-tooltip label="HiHi" width="100" sortable='custom'></el-table-column>
                <el-table-column prop="hiAddr"  show-overflow-tooltip label="Hi" width="100" sortable='custom'></el-table-column>
                <el-table-column prop="loAddr"  show-overflow-tooltip label="Lo" width="100" sortable='custom'></el-table-column>
                <el-table-column prop="loloAddr"  show-overflow-tooltip label="LoLo" width="100" sortable='custom'></el-table-column> -->
        <el-table-column prop="description" show-overflow-tooltip :label="$t('common.describe')" width="180">
          <template slot-scope="scope">
            {{scope.row.phaseTagDefineVo ? scope.row.phaseTagDefineVo.description : ""}}
          </template>
        </el-table-column>
        <el-table-column prop="tagAddr" show-overflow-tooltip :label="$t('basic.argu_addr')"></el-table-column>

      </el-table>
    </div>
    <div class="pagination-area">
      <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10,20, 30]"
        :page-size="pager.pageSize" :current-page="pager.pageNum" layout="total, sizes, prev, pager, next, jumper" :total="pager.total">
      </el-pagination> -->
    </div>

    <el-dialog v-dialogDrag :title="title == 1 ? $t('common.add') :  $t('common.edit')" :visible.sync="isShowDialog"
      :close-on-click-modal="false" :width="screenWidth > 1024 ? '30%' : '50%'" class="isShowDialog" :before-close="handleClose">
      <el-form :model="formData" label-width="85px" ref="ruleForm" size="small" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="35">
          <el-col :xs="24">
            <el-form-item :label="$t('basic.argu')" prop="phaseParam">
              <el-input v-model="formData.phaseParam" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <!--                    <el-col :xs="24">-->
          <!--                      <el-form-item label="读写" prop="wr" class="w-100">-->
          <!--                        <el-select v-model="formData.wr" placeholder="" clearable>-->
          <!--                              <el-option v-for="(item,index) in wrList" :key="index" :label="item.label" :value="item.value"></el-option>-->
          <!--                          </el-select>-->
          <!--                      </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                      <el-form-item :label="$t('basic.data_type')" prop="type" class="w-100">-->
          <!--                          <el-select v-model="formData.type" placeholder="" clearabl>-->
          <!--                              <el-option v-for="(item,index) in dataTypeList" :key="index" :label="item.label" :value="item.value"></el-option>-->
          <!--                          </el-select>-->
          <!--                      </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                      <el-form-item :label="$t('basic.company')" prop="uom" class="w-100">-->
          <!--                        <el-select v-model="formData.uom" placeholder="" clearable>-->
          <!--                            <el-option v-for="(item,index) in uomIdList" :key="index" :label="item.fid" :value="item.fid"></el-option>-->
          <!--                        </el-select>-->
          <!--                      </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                        <el-form-item label="HiHi" prop="hihiAddr" class="passLine">-->
          <!--                            <el-input type="number" v-enterNumber2  :disabled="!formData.hihiEnable" v-model="formData.hihiAddr" placeholder="">-->
          <!--                                <template slot="append">-->
          <!--                                    <el-checkbox v-model="formData.hihiEnable"></el-checkbox>-->
          <!--                                </template>-->
          <!--                            </el-input>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                        <el-form-item label="Hi" prop="hiAddr" class="passLine">-->
          <!--                            <el-input type="number" v-enterNumber2 :disabled="!formData.hiEnable" v-model="formData.hiAddr" placeholder="">-->
          <!--                                <template slot="append">-->
          <!--                                    <el-checkbox v-model="formData.hiEnable"></el-checkbox>-->
          <!--                                </template>-->
          <!--                            </el-input>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                        <el-form-item label="Lo" prop="loAddr" class="passLine">-->
          <!--                            <el-input type="number" v-enterNumber2 :disabled="!formData.loEnable" v-model="formData.loAddr" placeholder="">-->
          <!--                                <template slot="append">-->
          <!--                                    <el-checkbox v-model="formData.loEnable"></el-checkbox>-->
          <!--                                </template>-->
          <!--                            </el-input>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
          <!--                    <el-col :xs="24">-->
          <!--                        <el-form-item label="LoLo" prop="loloAddr" class="passLine">-->
          <!--                            <el-input type="number" v-enterNumber2 :disabled="!formData.loloEnable" v-model="formData.loloAddr" placeholder="">-->
          <!--                                <template slot="append">-->
          <!--                                    <el-checkbox v-model="formData.loloEnable"></el-checkbox>-->
          <!--                                </template>-->
          <!--                            </el-input>-->
          <!--                        </el-form-item>-->
          <!--                    </el-col>-->
          <el-col :xs="24">
            <el-form-item :label="$t('basic.argu_addr')" prop="tagAddr" class="w-100">
              <el-select v-model="formData.tagAddr" placeholder="" clearable>
                <el-option v-for="(item,index) in addrTagFilter" :key="index" :label="item.addr" :value="item.addr"></el-option>
              </el-select>
              <!-- <el-input-number v-model="formData.tagAddr" :precision="1" :step="0.1" :min="0" :max="maxNum" :disabled="disabled"></el-input-number> -->
              <!-- <el-input v-model="formData.tagAddr" placeholder="" clearable></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleCreate('ruleForm')"
          :loading="loading">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { configure } from "@/api/configure"
export default {
  props: {
    screenWidth: Number,
    radioId: String,
    phaseType: [String, Number]
  },
  data () {
    let hihiFn = (rule, value, callback) => {
      if (this.formData.hihiEnable && !value) {
        callback(new Error('请输入HiHi'));
      } else {
        callback();
      }
    }
    let hiFn = (rule, value, callback) => {
      if (this.formData.hiEnable && !value) {
        callback(new Error('请输入Hi'));
      } else {
        if (this.formData.hihiEnable && (parseFloat(value) >= parseFloat(this.formData.hihi))) {
          callback(new Error('Hi不能大于等于HiHi'));
        } else {
          callback();
        }
      }
    }
    let loFn = (rule, value, callback) => {
      if (this.formData.loEnable && !value) {
        callback(new Error('请输入Lo'));
      } else {
        if (this.formData.hiEnable && (parseFloat(value) >= parseFloat(this.formData.hi))) {
          callback(new Error('Lo不能大于等于Hi'));
        } else if (!this.formData.hiEnable && this.formData.hihiEnable && (parseFloat(value) >= parseFloat(this.formData.hihi))) { //如果hi没填  hihi填了 则去和hihi比较
          callback(new Error('Lo不能大于等于HiHi'));
        } else {
          callback();
        }
      }
    }
    let loloFn = (rule, value, callback) => {
      if (this.formData.loloEnable && !value) {
        callback(new Error('请输入LoLo'));
      } else {
        if (this.formData.loEnable && (parseFloat(value) >= parseFloat(this.formData.lo))) {
          callback(new Error('LoLo不能大于等于Lo'));
        } else if (!this.formData.loEnable && this.formData.hiEnable && (parseFloat(value) >= parseFloat(this.formData.hi))) {
          callback(new Error('LoLo不能大于等于Hi'));
        } else if (!this.formData.loEnable && !this.formData.hiEnable && this.formData.hihiEnable && (parseFloat(value) >= parseFloat(this.formData.hihi))) {
          callback(new Error('LoLo不能大于等于HiHi'));
        } else {
          callback();
        }
      }
    }
    return {
      // screenWidth: document.body.clientWidth,
      tableData: [],
      tableLoading: false,
      height: 32 * 6.5,
      childId: "",
      currObj: {},
      pager: {
        pageNum: 1,
        pageSize: 555,
        total: 0
      },
      formData: {
        // name:"", //参数
        // wr:"", //读写
        type: "", //数据类型
        // uom:"", //单位
        // hihiAddr:"",
        // hiAddr:"",
        // loAddr:"",
        // loloAddr:"",
        // hihiEnable:false,
        // hiEnable:false,
        // loloEnable:false,
        // loEnable:false,
        phaseParam: '',
        tagAddr: "" //地址
      },
      uomIdList: [],
      title: 1,
      isShowDialog: false,
      wrList: [{
        label: "读写",
        value: 0
      }, {
        label: "只读",
        value: 1
      }, {
        label: "只写",
        value: 2
      }],
      dataTypeList: [{
        label: "Float",
        value: 0
      }, {
        label: "Int",
        value: 1
      }, {
        label: "String",
        value: 2
      }, {
        label: "Bool",
        value: 3
      }, {
        label: "枚举",
        value: 4
      }],
      rules: {
        phaseParam: [{ required: true, message: '参数不能为空', trigger: 'blur' }],
        wr: [{ required: true, message: '读写不能为空', trigger: 'change' }],
        type: [{ required: true, message: '数据不能为空', trigger: 'change' }],
        uom: [{ required: true, message: '单位不能为空', trigger: 'change' }],
        hihiAddr: [{ validator: hihiFn, trigger: 'blur' }],
        hiAddr: [{ validator: hiFn, trigger: 'blur' }],
        loAddr: [{ validator: loFn, trigger: 'blur' }],
        loloAddr: [{ validator: loloFn, trigger: 'blur' }],
      },
      loading: false,
      maxNum: 0,
      disabled: false,
      addrTagList: [],
      addrTagFilter: []
    }
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    },
    radioId (val) {
      this.unitPhaseTagsDefineList()
      this.pager.pageNum = 1
      this.pager.total = 0
      this.childId = ''
    }
  },
  created () {
    this.getAddrTagList()
  },
  methods: {
    rowClick (row) {
      this.childId = row.fid
      this.currObj = row;
      if (row.phaseTagDefineVo) {
        // let tagCount = this.tableData.length
        let tagCount = 30
        this.addrTagFilter = []
        // for (let ii = 0; ii < tagCount; ii++) {
        //   this.addrTagFilter.push({ 'addr': (ii > 9 ? ii : '0' + ii) })
        // }

        if (row.phaseTagDefineVo.type == 2) { //只读和string类型的数据
          this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'W' && item.tagType == 8)
        } else if (row.phaseTagDefineVo.type != 2) { //只读和不是string类型的数据
          this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'W' && item.tagType != 8)
        }

        // if (row.phaseTagDefineVo.wr == 1 && row.phaseTagDefineVo.type == 2) { //只读和string类型的数据
        //   this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'R' && item.tagType == 8)
        // } else if (row.phaseTagDefineVo.wr == 1 && row.phaseTagDefineVo.type != 2) { //只读和不是string类型的数据
        //   this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'R' && item.tagType != 8)
        // } else if ((row.phaseTagDefineVo.wr == 0 || row.phaseTagDefineVo.wr == 2) && row.phaseTagDefineVo.type == 2) { //不是只读和是string类型
        //   this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'W' && item.tagType == 8)
        // } else if ((row.phaseTagDefineVo.wr == 0 || row.phaseTagDefineVo.wr == 2) && row.phaseTagDefineVo.type != 2) { //不是只读和不是string类型
        //   this.addrTagFilter = this.addrTagList.filter(item => item.wr == 'W' && item.tagType != 8)
        // }
      }
      this.$refs.refTable.setCurrentRow(row);

    },
    unitPhaseTagsDefineList () {
      const param = {
        phaseId: this.radioId,
        page: this.pager.pageNum,
        size: this.pager.pageSize,
        field: 'tagAddr',
        order: 'asc'
      }
      configure.unitPhaseTagsDefineList(param).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.rows
          this.pager.total = res.data.total
          this.tableData.forEach(ii => {
            if (isNaN(ii.tagAddr)) {
              ii.tagAddr = ii.tagAddr.replace('PP_', '')
              ii.tagAddr = ii.tagAddr.replace('PPS_', '')
            }
          })
          console.log(this.tableData);
        }
      })
    },
    sortChange (column) {
      if (column.order === 'descending') {
        this.pager.field = column.prop
        this.pager.order = 'DESC'
      } else {
        this.pager.field = column.prop
        this.pager.order = 'ASC'
      }
      this.pager.pageNum = 1
    },
    table_index (index) {
      return (this.pager.pageNum - 1) * this.pager.pageSize + index
    },
    getDataType (id) {
      if (!id && id !== 0) return ''
      return this.dataTypeList.find(item => item.value === id).label
    },
    getwrName (id) {
      if (!id && id !== 0) return ''
      return this.wrList.find(item => item.value === id).label

    },
    handleAdd () {
      if (this.radioId === '') {
        this.$message.error('请选择阶段列表');
        return
      }
      this.title = 1
      try {
        this.$refs['ruleForm'].resetFields();
      } catch (e) { }
      this.isShowDialog = true
    },
    async handleEdit () {
      if (this.childId === '') {
        this.$message.error(this.$t('dialog.please_sel_edit'));
        return
      }
      let res = await configure.findUnitPhaseTagsDefine({ fid: this.childId })

      if (res.code == 200) {
        this.formData = Object.assign({}, res.data)
        if (isNaN(this.formData.tagAddr)) {
          this.formData.tagAddr = this.formData.tagAddr.replace('PP_', '')
          this.formData.tagAddr = this.formData.tagAddr.replace('PPS_', '')
        }

        this.formData.phaseParam = this.currObj.phaseTagDefineVo.name
        this.handleSelectLink(this.phaseType)
        this.title = 2
        this.isShowDialog = true
      } else {
        this.$message.error('暂无此数据');
      }
    },
    handleDel () {

    },
    handleClose () {
      this.isShowDialog = false
    },
    handleSizeChange (val) {
      this.pager.pageNum = 1
      this.pager.pageSize = val
      this.unitPhaseTagsDefineList()
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.unitPhaseTagsDefineList()
    },
    // 获取计量单位列表
    getUomList () {
      const param = {
        page: 1,
        size: 9999
      }
      configure.getUomList(param).then(res => {
        if (res.code == 200) {
          this.uomIdList = res.data.rows
        }
      }).catch(e => {

      })
    },
    handleCreate (formName) {
      console.log(this.currObj)
      let isHave = false
      this.tableData.forEach(ii => {

        if (ii.tagAddr == this.formData.tagAddr.replace('PP_', '') && ii.fid != this.formData.fid) {
          this.$message.error('已占用该参数地址');
          // this.isShowDialog = true
          isHave = true
          return
        }
      })
      if (isHave) return
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.formData)
          param.hihiEnable = this.formData.hihiEnable === false ? 0 : 1
          param.hiEnable = this.formData.hiEnable === false ? 0 : 1
          param.loEnable = this.formData.loEnable === false ? 0 : 1
          param.loloEnable = this.formData.loloEnable === false ? 0 : 1
          param.linkId = this.currObj.linkId
          console.log(param)
          if (this.currObj.phaseTagDefineVo.type == 2) {
            if (!isNaN(param.tagAddr)) param.tagAddr = "PPS_" + param.tagAddr
          } else {
            if (!isNaN(param.tagAddr)) param.tagAddr = "PP_" + param.tagAddr
          }
          this.loading = true
          configure.unitPhaseTagsDefineAdd(param, this.title).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$refs[formName].resetFields();
              this.unitPhaseTagsDefineList()
              this.isShowDialog = false
            }
          }).catch(e => {
            this.loading = false
            this.$message.error('服务器繁忙,请稍后再试');
          })
        } else {
          return false
        }
      })
    },
    getAddrTagList () {
      configure.getAddrTagList().then(res => {
        if (res.code == 200) {
          this.addrTagList = res.data.rows
        }
      })
    },
    handleSelectLink (type) {
      /**
       * item.type
       * 1 全自动
       * 7 全手动
       * 9 自动加料
       * 10 自动转料
       * 
       * 2 数据
       * 
       * 3 信息提示
       * 4 物料录入
       * 6 物料产出
       * 5 数据录入
       */
      let arr = [1, 7, 9, 10] //自动
      let arr1 = [3, 4, 5, 6]
      if (arr.indexOf(type) > -1) {
        this.maxNum = 11
        this.disabled = false
      } else if (type == 2) {
        this.maxNum = 11
        this.disabled = false
      } else if (arr1.indexOf(type) > -1) {
        this.maxNum = 0
        this.disabled = true
      }
    },
  }
}
</script>

<style scoped>
</style>